<?php

/**
 * Created by PhpStorm.
 * User: li
 * Date: 15-10-18
 * Time: 下午10:59
 */
class Register extends MY_View_Controller
{
    function __construct(){
        parent::__construct();
    }

    function index(){
        $this->display();
    }

    /**
     * 注册接口
     */
    function do_register(){
        $this->load->helper('validate');
        if($this->input->post('mobile')){
            $username = $this->input->post('mobile');
            $type = 'mobile';
        }else{
            $username = $this->input->post('email');
            $type = 'email';
        }
        if(!$username)
            $this->json_response(false,'','用户名不可用');
        $password = trim($this->input->post('password'));
        $re_password = trim($this->input->post('re_password'));
        if($password!=$re_password)
            $this->json_response(false,'','确认密码错误');
        $data = array(
            'username'  =>  $username,
            $type       =>  $username,
            'password'  =>  $password
        );
        $device_id = $this->input->post('device_id');
        $device = $this->input->post('device');
        if($device_id){
            $data['device_id'] = $device_id;
            $data['device'] = $device;
        }
        $action =  '_user_'.$type;
        $this->{$action}($data);

    }
    private function _user_email($data){
        $check_email = is_email($data['email']);
        if(!$check_email['status'])
            $this->json_response(false,null,'邮箱不可用');
        $user = $this->auth_nuan->create_user(
            $data['username'],'email',$data['email'],$data['password'],
            array(
                'device_id' =>  @$data['device_id'],
                'device'    =>  @$data['device']
            )
        );
        if($user)
            $this->json_response(true,array_merge($user,$this->auth_nuan->get_error_message()),'注册成功');
        $this->json_response(false);
    }
    private function _user_mobile($data){
        $check_mobile = is_mobile($data['mobile']);
        if(!$check_mobile['status'])
            $this->json_response(false,null,'手机不可用');
        $user = $this->auth_nuan->create_user(
            $data['username'],'mobile',$data['mobile'],$data['password'],
            array(
                'device_id' =>  @$data['device_id'],
                'device'    =>  @$data['device']
            )
        );
        if($user)
            $this->json_response(true,array_merge($user,$this->auth_nuan->get_error_message()),'注册成功');
        $this->json_response(false);
    }

    /**
     * 检测手机是否可用
     */
    public function check_mobile(){
        $mobile = $this->input->post('mobile');
        if ($mobile) {
            $this->load->model('auth/users');
            if ($this->Users->is_mobile_available($mobile)) {
                $this->json_response(true);
            } else {
                $this->json_response(false,'','手机号不可用');
            }
        }
    }

    /**
     * 发送验证码
     */
    function send_verify(){
        $mobile = $this->input->post('mobile');
        //$this->_check_mobile($mobile);

        $this->load->helper('string');
        $this->load->helper('sms');

        $verify = random_string('numeric',6);
        $flag = send_sms($mobile,$verify,'register');
        if($flag){
            $_SESSION['register'] = array('mobile'=>$mobile,'verify'=>md5($verify));
            $this->json_response(true);
        }
        $this->json_response(false,'','发送验证码失败');
    }

    /**
     * 检测验证码
     */
    function check_verify(){
        $mobile = $this->input->post('mobile');
        echo $verify = $this->input->post('verify');
        $data   = @$_SESSION['register'];
        if ($mobile==$data['mobile']) {
            if (md5($verify)==$data['verify']) {
                $this->json_response(true);
            } else {
                $this->json_response(false,null,'验证码错误');
            }
        }else{
            $this->json_response(false,null,'没有验证码');
        }
    }

}